package cn.etynit.web.controller;

import cn.etynit.dto.AccountLoginDto;
import cn.etynit.dto.PayDto;
import cn.etynit.dto.RechargeDto;
import cn.etynit.dto.VisitorDto;
import cn.etynit.service.IUserService;
import cn.etynit.domain.User;
import cn.etynit.query.UserQuery;
import cn.etynit.result.JsonResult;
import cn.etynit.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Api(value = "业主接口",description = "此接口是业主相关的类")
public class UserController {

    @Autowired
    public IUserService userService;

    /**
    * 保存和修改公用的
    */
    @ApiOperation(value = "保存和修改业主",notes = "保存和修改业主",response = JsonResult.class)
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody User user){
        if(user.getId()!=null){
            userService.updateById(user);
        }else{
            userService.insert(user);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @ApiOperation(value = "删除业主",notes = "删除业主",response = JsonResult.class)
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id){
        userService.deleteById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @ApiOperation(value = "根据id查询业主",notes = "根据id查询业主",response = JsonResult.class)
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success(userService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @ApiOperation(value = "查询所有业主",notes = "查询所有业主",response = JsonResult.class)
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult list(){
        return JsonResult.success(userService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @ApiOperation(value = "分页查询业主",notes = "分页查询业主",response = JsonResult.class)
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JsonResult page(@RequestBody UserQuery query){
        Page<User> page = new Page<User>(query.getPage(),query.getRows());
        page = userService.selectPage(page);
        return JsonResult.success(new PageList<User>(page.getTotal(),page.getRecords()));
    }

    /**
     * 添加业主
     * @param user
     * @return
     */
    @ApiOperation(value = "业主注册",notes = "业主注册",response = JsonResult.class)
    @RequestMapping(value="/addingAnOwner",method= RequestMethod.POST)
    public JsonResult addingAnOwner(@RequestBody User user){
        return JsonResult.success(userService.addingAnOwner(user));
    }

    /**
     * 费用充值
     * @param rechargeDto
     * @return
     */
    @ApiOperation(value = "业主费用余额充值",notes = "业主费用余额充值",response = JsonResult.class)
    @RequestMapping(value="/ChargeTopUp",method= RequestMethod.POST)
    public JsonResult ChargeTopUp(@RequestBody RechargeDto rechargeDto,String token){
        return JsonResult.success(userService.ChargeTopUp(rechargeDto,token));
    }

    /**
     * 缴费
     * @param payDto
     * @return
     */
    @ApiOperation(value = "业主缴费",notes = "业主缴费",response = JsonResult.class)
    @RequestMapping(value="/Pay",method= RequestMethod.POST)
    public JsonResult Pay(@RequestBody PayDto payDto,String token){
        return JsonResult.success(userService.Pay(payDto,token));
    }

    /**
     * 根据业主id获取有效活动
     * @param token
     * @return
     */
    @ApiOperation(value = "根据业主id获取有效活动",notes = "根据业主id获取有效活动",response = JsonResult.class)
    @RequestMapping(value = "/getEventNotification",method = RequestMethod.GET)
    public JsonResult getEventNotification(String token){
        return JsonResult.success(userService.getEventNotification(token));
    }

    /**
     * 访客进入
     * @param visitorDto
     * @return
     */
    @ApiOperation(value = "访客进小区",notes = "访客进小区",response = JsonResult.class)
    @RequestMapping(value="/visitor",method= RequestMethod.POST)
    public JsonResult visitor(@RequestBody VisitorDto visitorDto){
        return JsonResult.success(userService.visitor(visitorDto));
    }

    /**
     * 访客离开
     * @param name
     * @param phone
     * @return
     */
    @ApiOperation(value = "访客离开小区",notes = "访客离开小区",response = JsonResult.class)
    @RequestMapping(value = "/visitorsLeave/{name}/{phone}",method = RequestMethod.GET)
    public JsonResult visitorsLeave(@PathVariable("name")String name,@PathVariable("phone")String phone){
        return JsonResult.success(userService.visitorsLeave(name,phone));
    }

    /**
     * 业主登录
     * @param accountLoginDto
     * @return
     */
    @ApiOperation(value = "业主登录",notes = "业主登录",response = JsonResult.class)
    @RequestMapping(value="/accountLogin",method= RequestMethod.POST)
    public JsonResult accountLogin(@RequestBody AccountLoginDto accountLoginDto){
        return JsonResult.success(userService.accountLogin(accountLoginDto));
    }

    /**
     * 查询通知
     * @param token
     * @return
     */
    @ApiOperation(value = "查询有效通知",notes = "查询有效通知",response = JsonResult.class)
    @RequestMapping(value = "/viewNotifications/{token}",method = RequestMethod.GET)
    public JsonResult viewNotifications(@PathVariable("token") String token){
        return JsonResult.success(userService.viewNotifications(token));
    }

}
